#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include"xh.h"
list createlist() {
	position L = (position)malloc(sizeof(struct Node));
		if (L == NULL) {
			perror("malloc");
			return NULL;
		}

		L->Next = NULL;
		return L;
}

int isempty(list L) {
	return L->Next ==NULL;
}

position find(ElementType x,list L) {
	position p = L->Next ;
	while (p!= NULL && p->element != x) {
		p = p->Next;
	}
	return p;
}

position findprevious(ElementType x, list L) {
	position p= L;
	while (p->Next != NULL && p->Next->element != x) {
		p = p->Next;
	}
	return p;
}

void insert(ElementType x, list L, position p) {
	position tmpcell = (position)malloc(sizeof(struct Node));
	if (tmpcell == NULL) {
		perror("malloc");
	}
	tmpcell->element = x;
	tmpcell->Next = p->Next ;
	p->Next = tmpcell;
}

void insertfrist(ElementType x, list L) {
	position tmpcell = (position)malloc(sizeof(struct Node));
	if (tmpcell == NULL) {
		perror("malloc");
	}
	tmpcell->element = x;
	tmpcell->Next = L->Next;
	L->Next = tmpcell;
}

void insertlast(ElementType x, list L) {
	position last = isempty(L);
	if (last!= NULL) {
		insert(x, L, last);
	}
	position p= (position)malloc(sizeof(struct Node));
	p->element = x;
	p->Next = NULL;
	L->Next=p;
}

void deletNode(ElementType x,list L) {
	position prv, tmpcell;
	prv = findprevious(x, L);
	if (prv!=NULL) {
		tmpcell = prv->Next;
		prv->Next = prv->Next->Next;
		free(tmpcell);
	}
}

position last(list L) {
	if (isempty) {
		return NULL;
	}
	position p = L->Next;
	while (p!= NULL && p->Next != NULL) {
		p = p->Next;
	}
	return p;
}

position Advance(position P) {
	return P == NULL ? NULL : P->Next;
}

void print(list list) {
	position p = list->Next;
	while (p !=NULL) {
		printf("%d ", p->element);
		p = p->Next;
	}
}